package com.banzhi.shop.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.RefundRequest.Refund_Data;
import com.banzhi.shop.entity.ShouhouRequest.Refunddata;
import com.banzhi.shop.model.CallBack.InputPicturesCallBack;
import com.banzhi.shop.model.CallBack.InputRefuntCallBack;
import com.banzhi.shop.model.IInputPictureModel;
import com.banzhi.shop.model.IInputRequestModel;
import com.banzhi.shop.model.Impl.ChoiceInputRequestFrgImpl;
import com.banzhi.shop.model.Impl.InputPictureImpl;
import com.banzhi.shop.model.Impl.InputRequestFrgImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.BitmapUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.NumberUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.squareup.picasso.Picasso;

import org.json.JSONObject;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
/**
 *     申请售后
 */
public class ChoiceRefundRequestActivity extends BaseActivity implements View.OnClickListener, InputPicturesCallBack, InputRefuntCallBack {
    private ImageView ivback;
    /**
     * 选择退款类型
     */
    private RelativeLayout rlrefundtype;
    /**
     * 显示退款类型的文本
     */
    private TextView tvrefundtype;
    /**
     * 选择退款原因
     */
    private RelativeLayout rlreson;
    /**
     * 显示退款类型的原因
     */
    private TextView tvreson;
    private Refund_Data Refunddata;
    /**
     * 输入退款金额
     */
    private EditText etbackmoney;
    /**
     * 输入退款说明
     */
    private EditText etshuoming;
    private Button btn_save;
    private TextView tvmoney;
    private ImageView ivpic1;
    private ImageView ivpic2;
    private ImageView ivpic3;
    /**
     * 裁剪后的照片文件
     */
    private File Imagefile;
    /**
     * 上传头像后返回的头像的网络地址
     */
    private String imageURL1;
    private String imageURL2;
    private String imageURL3;

    private Bitmap bitmap;
    private File dir;

    private IInputPictureModel picmodel;
  //  private IInputRequestModel requestModel;
    /**
     * 提交的图片们
     */
    private String images = null;
    /**
     * 传过来的订单id
     */
    private String Orderid;
    /**
     * 传过来的我的退款信息
     */
    private Refunddata refunddata;

    private RelativeLayout rlshangping;
    private TextView tvshangping;
    private EditText etReturnNum;
    private List<String> shangPingList = new ArrayList<>();

    private LinearLayout lldanjian;
    private TextView tvdanjian_price;
    private TextView tvdanjian_num;

    /**
     * 订单状态
     */
    private String status;

    //描述用户的选择信息
    private String goodsid;
    private String goodsNum;
    private ChoiceInputRequestFrgImpl requestModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund_request);
        Translucent.setTranslucent(this);

        Intent intent = getIntent();
        Refunddata = (Refund_Data) intent.getSerializableExtra("data");
        Orderid = intent.getStringExtra("Orderid");
        status = intent.getStringExtra("status");
        refunddata = (Refunddata) intent.getSerializableExtra("Refunddata");
        setinits();
        setListeners();
        picmodel = new InputPictureImpl();
      //  requestModel = new InputRequestFrgImpl();
        requestModel = new ChoiceInputRequestFrgImpl();
    }

    private void setinits() {

        tvmoney = (TextView) findViewById(R.id.tv_money);
        if (Refunddata != null) {
            if (ASUtils.isCanUse(Refunddata.getPrice())) {
                tvmoney.setText(NumberUtils.DecimalFormat(Refunddata.getPrice()));
            }
        }
        ivback = (ImageView) findViewById(R.id.iv_back);
        rlrefundtype = (RelativeLayout) findViewById(R.id.rl_refund_type);
        tvrefundtype = (TextView) findViewById(R.id.tv_refund_type);
        rlreson = (RelativeLayout) findViewById(R.id.rl_reson);
        tvreson = (TextView) findViewById(R.id.tv_reson);
        etbackmoney = (EditText) findViewById(R.id.et_backmoney);
        etshuoming = (EditText) findViewById(R.id.et_shuoming);
        btn_save = (Button) findViewById(R.id.btn_save);
        etbackmoney.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        ivpic1 = (ImageView) findViewById(R.id.iv_pic1);
        ivpic2 = (ImageView) findViewById(R.id.iv_pic2);
        ivpic3 = (ImageView) findViewById(R.id.iv_pic3);
        rlshangping = (RelativeLayout) findViewById(R.id.rl_shangping);
        tvshangping = (TextView) findViewById(R.id.tv_shangping);
        etReturnNum = (EditText) findViewById(R.id.et_return_Num);
        lldanjian = (LinearLayout) findViewById(R.id.ll_danjian);
        tvdanjian_price = (TextView) findViewById(R.id.tv_danjian_danjia);
        tvdanjian_num = (TextView) findViewById(R.id.tv_danjian_num);

        if (refunddata != null) {
            if ("0".equals(refunddata.getRtype())) {
                tvrefundtype.setText("退款");
            } else if ("1".equals(refunddata.getRtype())) {
                tvrefundtype.setText("退货退款");
            }
            if ("2".equals(refunddata.getRtype())) {
                tvrefundtype.setText("换货");
            }
            tvreson.setText(refunddata.getReason());
            etbackmoney.setText(refunddata.getApplyprice());
            etshuoming.setText(refunddata.getContent());
            if (ASUtils.isCanUse(refunddata.getImgs())) {
                String[] strings = refunddata.getImgs().split(",");
                if (strings.length > 0) {
                    imageURL1 = strings[0];
                    Picasso.with(this).load(imageURL1).placeholder(R.mipmap.zhanweitu2).into(ivpic1);
                    ivpic2.setVisibility(View.VISIBLE);
                }
                if (strings.length > 1) {
                    imageURL2 = strings[1];
                    Picasso.with(this).load(imageURL2).placeholder(R.mipmap.zhanweitu2).into(ivpic2);
                    ivpic3.setVisibility(View.VISIBLE);
                }
                if (strings.length > 2) {
                    imageURL3 = strings[2];
                    Picasso.with(this).load(imageURL3).placeholder(R.mipmap.zhanweitu2).into(ivpic3);
                }
            }
        }
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        rlrefundtype.setOnClickListener(this);
        rlreson.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        ivpic1.setOnClickListener(this);
        ivpic2.setOnClickListener(this);
        ivpic3.setOnClickListener(this);
        rlshangping.setOnClickListener(this);
    }

    /**
     * 用于记录是第几张图片的路径
     */
    private String type;
    /**
     * 用于记录是哪个售后类型  值0退款，值1退货退款，值2换货
     */
    private String num = "0";

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_refund_type:
                if (Refunddata != null) {
                    showTypePopupwindow(Refunddata.getRefundType(), rlrefundtype, tvrefundtype);
                }
                break;
            case R.id.rl_reson:
                if (Refunddata != null) {
                    showTypePopupwindow(Refunddata.getReason(), rlreson, tvreson);
                }
                break;
            case R.id.rl_shangping:
                if ("1".equals(status)) {
                    tvshangping.setText("全部商品");
                    etReturnNum.setFocusable(false);
                    etReturnNum.setFocusableInTouchMode(false);
                    goodsid = "0";
                } else {
                    if (Refunddata != null) {
                        shangPingList.clear();
                        for (Refund_Data.Goods goods : Refunddata.getGoods()) {
                            shangPingList.add(goods.getTitle());
                        }
                        showTypePopupwindow(shangPingList, rlshangping, tvshangping);
                    }
                }

                break;
            case R.id.btn_save:
                String leixing = tvrefundtype.getText().toString().trim();
                String reson = tvreson.getText().toString().trim();
                String backmoney = etbackmoney.getText().toString().trim();
                if (".".equals(backmoney)) {
                    backmoney = "0";
                }
                String shuoming = etshuoming.getText().toString().trim();
                if (!ASUtils.isCanUse(leixing)) {
                    ToastUtils.makeToastShort("请先选择退款类型");
                } else if (!ASUtils.isCanUse(reson)) {
                    ToastUtils.makeToastShort("请先选择退款原因");
                } else if (!ASUtils.isCanUse(tvshangping.getText().toString().trim())) {
                    ToastUtils.makeToastShort("请先选择退款商品");
                } else {
                    images = null;
                    if (ASUtils.isCanUse(imageURL1)) {
                        images = imageURL1 + ",";
                    }
                    if (ASUtils.isCanUse(imageURL2)) {
                        if (images == null) {
                            images = imageURL2 + "";
                        } else {
                            images = images + imageURL2 + ",";
                        }
                    }
                    if (ASUtils.isCanUse(imageURL3)) {
                        if (images == null) {
                            images = imageURL3 + "";
                        } else {
                            images = images + imageURL3 + ",";
                        }
                    }
                    if (images != null) {
                        images = images.substring(0, images.length() - 1);
                    }

                    if ("退款(仅退款不退货)".equals(tvrefundtype.getText().toString().trim()) || "退款".equals(tvrefundtype.getText().toString().trim())) {
                        num = "0";
                    } else if ("退货退款".equals(tvrefundtype.getText().toString().trim())) {
                        num = "1";
                    } else if ("换货".equals(tvrefundtype.getText().toString().trim())) {
                        num = "2";
                    }
                    if (!"2".equals(num) && !ASUtils.isCanUse(backmoney)) {
                        ToastUtils.makeToastShort("请先输入退款金额");
                        return;
                    } else {
                        if (ASUtils.isCanUse(backmoney) && Double.parseDouble(backmoney) > Double.parseDouble(tvmoney.getText().toString().trim())) {
                            ToastUtils.makeToastShort("退款金额不能大于最大金额");
                            return;
                        }
                    }
                    if ("0".equals(goodsid)) {
                        goodsNum = null;
                    } else {
                        if (!ASUtils.isCanUse(etReturnNum.getText().toString().trim())) {
                            ToastUtils.makeToastShort("请输入退款商品数量");
                            return;
                        }
                        if (Integer.parseInt(etReturnNum.getText().toString().trim()) > Integer.parseInt(goodsNum)) {
                            ToastUtils.makeToastShort("输入的商品数量超过最大值");
                            return;
                        }

                    }

                    if (ASUtils.isCanUse(num)) {
                        if (refunddata != null) {
                            requestModel.inptu(refunddata.getId(), Orderid, num, tvreson.getText().toString().trim(), etbackmoney.getText().toString().trim(), shuoming, images, goodsid,
                                    etReturnNum.getText().toString().trim(), this);
                        } else {
                            requestModel.inptu(Orderid, num, tvreson.getText().toString().trim(), etbackmoney.getText().toString().trim(), shuoming, images,
                                    goodsid, etReturnNum.getText().toString().trim(), this);
                        }
                    }
                }

                break;
            case R.id.iv_pic1:
                type = "pic1";
                showImagePopwindow();
                break;
            case R.id.iv_pic2:
                type = "pic2";
                showImagePopwindow();
                break;
            case R.id.iv_pic3:
                type = "pic3";
                showImagePopwindow();
                break;
        }

    }

    @Override
    public void inputSuccess(Object object, String type) {
        JSONObject jsonObject = (JSONObject) object;
        if ("pic1".equals(type)) {
            imageURL1 = jsonObject.optString("res");
            if (ASUtils.isCanUse(imageURL1)) {
                Picasso.with(this).load(imageURL1).placeholder(R.mipmap.zhanweitu2).into(ivpic1);
                ivpic2.setVisibility(View.VISIBLE);
            }
//            else {
//                ivpic1.setImageBitmap(bitmap); // 将裁剪后的照片显示出来
//            }
        } else if ("pic2".equals(type)) {
            imageURL2 = jsonObject.optString("res");
            if (ASUtils.isCanUse(imageURL2)) {
                Picasso.with(this).load(imageURL2).placeholder(R.mipmap.zhanweitu2).into(ivpic2);
                ivpic3.setVisibility(View.VISIBLE);
            }
//            else {
//                ivpic2.setImageBitmap(bitmap); // 将裁剪后的照片显示出来
//            }
        } else if ("pic3".equals(type)) {
            imageURL3 = jsonObject.optString("res");
            if (ASUtils.isCanUse(imageURL3)) {
                Picasso.with(this).load(imageURL3).placeholder(R.mipmap.zhanweitu2).into(ivpic3);
            } else {
                ivpic3.setImageBitmap(bitmap); // 将裁剪后的照片显示出来
            }
        }

    }

    @Override
    public void inputError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * 申请售后成功
     *
     * @param object
     */
    @Override
    public void inputRequestSuccess(Object object) {
        if (refunddata == null) {
            showPopupwindow();
//            Intent intent = new Intent(ChoiceRefundRequestActivity.this, MyShouhouActivity.class);
//            startActivity(intent);
        } else {
            JSONObject jsonObject = (JSONObject) object;
            ToastUtils.makeToastShort(jsonObject.optString("msg"));
            finish();
        }
    }

    /**
     * 申请售后失败
     *
     * @param object
     */
    @Override
    public void inputRequestError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    private static final int CODE_GALLERY_REQUEST = 0x1;
    private static final int CODE_CAMERA_REQUEST = 0x2;
    private static final int CROP_PICTURE_REQUEST = 0x3;

    private static final String TEMP_FILE_NAME = "temp_icon.jpg";

    /**
     * Save the path of photo cropping is completed
     */
    private Uri icon_path;
    private Uri camera_path;

    /**
     * 选择头像
     */
    private void showImagePopwindow() {
        View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow_image, null);
        final PopupWindow popWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
        popWindow.setContentView(contentView);

        Button btnCamera = (Button) contentView.findViewById(R.id.btn_camera_pop_camera);
        Button btnAlbum = (Button) contentView.findViewById(R.id.btn_camera_pop_album);
        Button btnCancel = (Button) contentView.findViewById(R.id.btn_camera_pop_cancel);
        popWindow.setAnimationStyle(R.style.AnimBottom);
        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        RelativeLayout rlbackground = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rlbackground.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_camera_pop_camera:
                        dir = getExternalFilesDir("user_icon");
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                            icon_path = FileProvider.getUriForFile(getApplicationContext(),
                                    getPackageName() + ".fileprovider", new File(dir, TEMP_FILE_NAME));
                            camera_path = FileProvider.getUriForFile(getApplicationContext(),
                                    getPackageName() + ".fileprovider", new File(dir, "camera_pic.jpg"));
                        } else {
                            icon_path = Uri.fromFile(new File(dir, TEMP_FILE_NAME));
                            camera_path = Uri.fromFile(new File(dir, "camera_pic.jpg"));
                        }
                        Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, camera_path);
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                            intentFromCapture.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        }
                        startActivityForResult(intentFromCapture, CODE_CAMERA_REQUEST);
                        break;
                    case R.id.btn_camera_pop_album:
                        dir = getExternalFilesDir("user_icon");
                        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
                            icon_path = FileProvider.getUriForFile(getApplicationContext(),
                                    getPackageName() + ".fileprovider", new File(dir, TEMP_FILE_NAME));
                            camera_path = FileProvider.getUriForFile(getApplicationContext(),
                                    getPackageName() + ".fileprovider", new File(dir, "camera_pic.jpg"));
                        } else {
                            icon_path = Uri.fromFile(new File(dir, TEMP_FILE_NAME));
                            camera_path = Uri.fromFile(new File(dir, "camera_pic.jpg"));
                        }
                        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//        Intent intentFromGallery = new Intent();
//        intentFromGallery.setType("image/*");
//        intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(intent, CODE_GALLERY_REQUEST);
                        break;
                    case R.id.btn_camera_pop_cancel:

                        break;

                }
                popWindow.dismiss();
            }
        };

        btnCamera.setOnClickListener(listener);
        btnAlbum.setOnClickListener(listener);
        btnCancel.setOnClickListener(listener);

        ColorDrawable dw = new ColorDrawable(0x70000000);
        popWindow.setBackgroundDrawable(dw);
        popWindow.showAtLocation(contentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_CANCELED) {
            return;
        }
        switch (requestCode) {
            case CODE_GALLERY_REQUEST:
//                cropImage(data.getData(), 450, 450, CROP_PICTURE_REQUEST);
                bitmap = decodeUriAsBitmap(data.getData());
                Imagefile = BitmapUtils.saveBitmapFile(BitmapUtils.comBitmap(bitmap, 0.2f), "camera");
                picmodel.getData(Imagefile, type, this);
                bitmap = null;
                break;
            case CODE_CAMERA_REQUEST:
                bitmap = decodeUriAsBitmap(camera_path);
                Imagefile = BitmapUtils.saveBitmapFile(BitmapUtils.comBitmap(bitmap, 0.2f), "camera");
                picmodel.getData(Imagefile, type, this);
                bitmap = null;
//                cropImage(camera_path, 450, 450, CROP_PICTURE_REQUEST);
                break;
            case CROP_PICTURE_REQUEST:
                bitmap = decodeUriAsBitmap(icon_path);
                Imagefile = BitmapUtils.saveBitmapFile(BitmapUtils.comBitmap(bitmap, 0.2f), "camera");
                picmodel.getData(Imagefile, type, this);
                bitmap = null;
                break;

        }
    }

    /**
     * According to the incoming a length-width ratio began to cut out pictures
     *
     * @param uri Image source
     */
    private void cropImage(Uri uri, int outputX, int outputY, int requestCode) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // Set the cutting
        intent.putExtra("crop", "true");
        // aspectX , aspectY :In proportion to the width of high
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX , outputY : High cutting image width
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, icon_path);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            addurlPram(this, intent, camera_path, icon_path);
        }
        startActivityForResult(intent, requestCode);
    }

    private void addurlPram(Activity activity, Intent intent, Uri... uris) {
        List<ResolveInfo> resInfoList = activity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolveInfo : resInfoList) {
            String packageName = resolveInfo.activityInfo.packageName;
            for (Uri uri : uris) {
                activity.grantUriPermission(packageName, uri,
                        Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
        }
    }

    private Bitmap decodeUriAsBitmap(Uri uri) {
        Bitmap bitmap = null;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        return bitmap;
    }


    private void showTypePopupwindow(final List<String> list, View view, final TextView tv) {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_refund_type, null);
        final PopupWindow popWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popWindow.setContentView(contentView);

        final RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        CommonAdapter<String> adapter = null;
        if (adapter == null) {
            adapter = new CommonAdapter<String>(ChoiceRefundRequestActivity.this, R.layout.item_refund_request, list) {
                @Override
                public void convert(ViewHolder holder, String s, int position) {
                    holder.setText(R.id.tv_text, (String) s);
                }
            };
        } else {
            adapter.notifyDataSetChanged();
        }
        adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                tv.setText((String) list.get(position));
                if (tvshangping.hashCode() == tv.hashCode()) {

                    goodsid = Refunddata.getGoods().get(position).getId();

                    if (ASUtils.isCanUse(Refunddata.getGoods().get(position).getPrice())) {
                        tvmoney.setText(NumberUtils.DecimalFormat(Refunddata.getGoods().get(position).getPrice()));
                    }
                    if ("0".equals(goodsid)) {   //全部商品
                        lldanjian.setVisibility(View.GONE);
                        etReturnNum.setText("");
                        etReturnNum.setFocusable(false);
                        etReturnNum.setFocusableInTouchMode(false);
                    } else {
                        goodsNum = Refunddata.getGoods().get(position).getTotal();
                        lldanjian.setVisibility(View.VISIBLE);
                        if (ASUtils.isCanUse(Refunddata.getGoods().get(position).getGoodsprice())) {
                            tvdanjian_price.setText(NumberUtils.DecimalFormat(Refunddata.getGoods().get(position).getGoodsprice()));
                        }
                        tvdanjian_num.setText(goodsNum);
                        etReturnNum.setFocusableInTouchMode(true);
                        etReturnNum.setFocusable(true);
                        etReturnNum.requestFocus();
                    }

                }
                popWindow.dismiss();
            }
        });
        recyclerView.setAdapter(adapter);
        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.showAsDropDown(view, 0, 0);
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setAlpha(1.0f, ChoiceRefundRequestActivity.this);
            }
        });
        setAlpha(0.8f, ChoiceRefundRequestActivity.this);
    }

    public void setAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }


    /**
     * 售后成功后的提示框
     */
    private void showPopupwindow() {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_shenhou_chenggong, null);

        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);

        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChoiceRefundRequestActivity.this, ChoiceMyShouhouActivity.class);
                startActivity(intent);
                popWindow.dismiss();
                finish();
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

}
